﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using VirtualDigibox.Entities;
using VirtualDigibox.Common;

namespace VirtualDigibox.BusinessService
{
    public static class TemplateManager
    {
        public static string GetVLCArguments(ScheduleItem scItem, Channel channel)
        {
            string args = "";

            string filrNameTemplate = SettingsManager.FileNameTemplate;
            filrNameTemplate = filrNameTemplate.Replace(Constants.FileNameTemplateKeys.Channel, channel.Name);
            filrNameTemplate = filrNameTemplate.Replace(Constants.FileNameTemplateKeys.BroadcastName, scItem.BroadcastName);
            filrNameTemplate = filrNameTemplate.Replace(Constants.FileNameTemplateKeys.Day, scItem.Time.Day.ToString());
            filrNameTemplate = filrNameTemplate.Replace(Constants.FileNameTemplateKeys.Month, scItem.Time.Month.ToString());
            filrNameTemplate = filrNameTemplate.Replace(Constants.FileNameTemplateKeys.Year, scItem.Time.Year.ToString());
            filrNameTemplate = filrNameTemplate.Replace(Constants.FileNameTemplateKeys.StartTimeHours, scItem.Time.Hour.ToString());
            filrNameTemplate = filrNameTemplate.Replace(Constants.FileNameTemplateKeys.StartTimeMinutes, scItem.Time.Minute.ToString());

            string fileName = ValidatedFileName(filrNameTemplate);



            args = SettingsManager.VLCArgs.Replace(Constants.VLCArgumentKeys.URL, channel.DigiTVURL);
            args = args.Replace(Constants.VLCArgumentKeys.FileName, SettingsManager.VideoSavePath + @"\" + fileName);
            
            return args;
        }

        private static string ValidatedFileName(string fileName)
        {
            char[] invalidCHars = Path.GetInvalidFileNameChars();
            foreach (char c in invalidCHars)
            {
                fileName = fileName.Replace(c.ToString(), "_");
            }
            return fileName;
        }
    }
}
